"""
@Author    : ghenyar
@Time      : 2025/8/28 13:14
@File      : schedulerTimer
@Desc      : 
"""
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.interval import IntervalTrigger

scheduler = AsyncIOScheduler()


def cron_job(cron_expression: str):
    """
    定时任务装饰器(cron 表达式)
    :param cron_expression: crontab格式
        * * * * * command_to_execute
        - - - - -
        | | | | |
        | | | | +---- 一周中的星期几 (0 - 7) (星期天为0或7)
        | | | +------ 月份 (1 - 12)
        | | +-------- 一个月中的第几天 (1 - 31)
        | +---------- 小时 (0 - 23)
        +------------ 分钟 (0 - 59)
    :return:
    """

    def decorator(func):
        scheduler.add_job(func, CronTrigger.from_crontab(cron_expression))
        return func

    return decorator


def interval_job(seconds: int):
    """
     定时任务装饰器(秒)
    :param seconds:  秒
    :return:
    """

    def decorator(func):
        scheduler.add_job(func, IntervalTrigger(seconds=seconds))
        return func

    return decorator


async def start_scheduler():
    """
    启动调度器
    :return:
    """
    scheduler.start()


async def stop_scheduler():
    """
    停止调度器
    :return:
    """
    scheduler.shutdown()